home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / tcsh / dist / sh.misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-21  |  7.9 KB  |  457 lines

  1. /* $Header: /home/hyperion/mu/christos/src/sys/tcsh-6.01/RCS/sh.misc.c,v 3.7 1991/10/12 04:23:51 christos Exp $ */
  2. /*
  3.  * sh.misc.c: Miscelaneous functions
  4.  */
  5. /*-
  6.  * Copyright (c) 1980, 1991 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37. #include "sh.h"
  38.  
  39. RCSID("$Id: sh.misc.c,v 3.7 1991/10/12 04:23:51 christos Exp $")
  40.  
  41. static    int    renum    __P((int, int));
  42.  
  43. /*
  44.  * C Shell
  45.  */
  46.  
  47. int
  48. any(s, c)
  49.     register char *s;
  50.     register int c;
  51. {
  52.     if (!s)
  53.     return (0);        /* Check for nil pointer */
  54.     while (*s)
  55.     if (*s++ == c)
  56.         return (1);
  57.     return (0);
  58. }
  59.  
  60. void
  61. setzero(cp, i)
  62.     char   *cp;
  63.     int     i;
  64. {
  65.     if (i != 0)
  66.     do
  67.         *cp++ = 0;
  68.     while (--i);
  69. }
  70.  
  71. char   *
  72. strsave(s)
  73.     register const char *s;
  74. {
  75.     char   *n;
  76.     register char *p;
  77.  
  78.     if (s == NULL)
  79.     s = (const char *) "";
  80.     for (p = (char *) s; *p++;);
  81.     n = p = (char *) xmalloc((size_t) ((p - s) * sizeof(char)));
  82.     while (*p++ = *s++);
  83.     return (n);
  84. }
  85.  
  86. Char  **
  87. blkend(up)
  88.     register Char **up;
  89. {
  90.  
  91.     while (*up)
  92.     up++;
  93.     return (up);
  94. }
  95.  
  96.  
  97. void
  98. blkpr(av)
  99.     register Char **av;
  100. {
  101.  
  102.     for (; *av; av++) {
  103.     xprintf("%s", short2str(*av));
  104.     if (av[1])
  105.         xprintf(" ");
  106.     }
  107. }
  108.  
  109. int
  110. blklen(av)
  111.     register Char **av;
  112. {
  113.     register int i = 0;
  114.  
  115.     while (*av++)
  116.     i++;
  117.     return (i);
  118. }
  119.  
  120. Char  **
  121. blkcpy(oav, bv)
  122.     Char  **oav;
  123.     register Char **bv;
  124. {
  125.     register Char **av = oav;
  126.  
  127.     while (*av++ = *bv++)
  128.     continue;
  129.     return (oav);
  130. }
  131.  
  132. Char  **
  133. blkcat(up, vp)
  134.     Char  **up, **vp;
  135. {
  136.  
  137.     (void) blkcpy(blkend(up), vp);
  138.     return (up);
  139. }
  140.  
  141. void
  142. blkfree(av0)
  143.     Char  **av0;
  144. {
  145.     register Char **av = av0;
  146.  
  147.     if (!av0)
  148.     return;
  149.     for (; *av; av++)
  150.     xfree((ptr_t) * av);
  151.     xfree((ptr_t) av0);
  152. }
  153.  
  154. Char  **
  155. saveblk(v)
  156.     register Char **v;
  157. {
  158.     register Char **newv =
  159.     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
  160.     Char  **onewv = newv;
  161.  
  162.     while (*v)
  163.     *newv++ = Strsave(*v++);
  164.     return (onewv);
  165. }
  166.  
  167. #ifndef POSIX
  168. char   *
  169. strstr(s, t)
  170.     register const char *s, *t;
  171. {
  172.     do {
  173.     register const char *ss = s;
  174.     register const char *tt = t;
  175.  
  176.     do
  177.         if (*tt == '\0')
  178.         return ((char *) s);
  179.     while (*ss++ == *tt++);
  180.     } while (*s++ != '\0');
  181.     return (NULL);
  182. }
  183.  
  184. #endif /* POSIX */
  185.  
  186. #ifndef SHORT_STRINGS
  187. char   *
  188. strspl(cp, dp)
  189.     char   *cp, *dp;
  190. {
  191.     char   *ep;
  192.     register char *p, *q;
  193.  
  194.     if (!cp)
  195.     cp = "";
  196.     if (!dp)
  197.     dp = "";
  198.     for (p = cp; *p++;);
  199.     for (q = dp; *q++;);
  200.     ep = (char *) xmalloc((size_t) (((p - cp) + (q - dp) - 1) * sizeof(char)));
  201.     for (p = ep, q = cp; *p++ = *q++;);
  202.     for (p--, q = dp; *p++ = *q++;);
  203.     return (ep);
  204. }
  205.  
  206. #endif
  207.  
  208. Char  **
  209. blkspl(up, vp)
  210.     register Char **up, **vp;
  211. {
  212.     register Char **wp =
  213.     (Char **) xcalloc((size_t) (blklen(up) + blklen(vp) + 1),
  214.               sizeof(Char **));
  215.  
  216.     (void) blkcpy(wp, up);
  217.     return (blkcat(wp, vp));
  218. }
  219.  
  220. Char
  221. lastchr(cp)
  222.     register Char *cp;
  223. {
  224.  
  225.     if (!cp)
  226.     return (0);
  227.     if (!*cp)
  228.     return (0);
  229.     while (cp[1])
  230.     cp++;
  231.     return (*cp);
  232. }
  233.  
  234. /*
  235.  * This routine is called after an error to close up
  236.  * any units which may have been left open accidentally.
  237.  */
  238. void
  239. closem()
  240. {
  241.     register int f;
  242.  
  243. #ifdef YPBUGS
  244.     /* suggested by Justin Bur; thanks to Karl Kleinpaste */
  245.     fix_yp_bugs();
  246. #endif
  247.     for (f = 0; f < NOFILE; f++)
  248.     if (f != SHIN && f != SHOUT && f != SHDIAG && f != OLDSTD &&
  249.         f != FSHTTY)
  250.         (void) close(f);
  251. }
  252.  
  253. #ifndef FIOCLEX
  254. /*
  255.  * Close files before executing a file.
  256.  * We could be MUCH more intelligent, since (on a version 7 system)
  257.  * we need only close files here during a source, the other
  258.  * shell fd's being in units 16-19 which are closed automatically!
  259.  */
  260. void
  261. closech()
  262. {
  263.     register int f;
  264.  
  265.     if (didcch)
  266.     return;
  267.     didcch = 1;
  268.     SHIN = 0;
  269.     SHOUT = 1;
  270.     SHDIAG = 2;
  271.     OLDSTD = 0;
  272.     isoutatty = isatty(SHOUT);
  273.     isdiagatty = isatty(SHDIAG);
  274.     for (f = 3; f < NOFILE; f++)
  275.     (void) close(f);
  276. }
  277.  
  278. #endif
  279.  
  280. void
  281. donefds()
  282. {
  283.  
  284.     (void) close(0);
  285.     (void) close(1);
  286.     (void) close(2);
  287.     didfds = 0;
  288. }
  289.  
  290. /*
  291.  * Move descriptor i to j.
  292.  * If j is -1 then we just want to get i to a safe place,
  293.  * i.e. to a unit > 2.  This also happens in dcopy.
  294.  */
  295. int
  296. dmove(i, j)
  297.     register int i, j;
  298. {
  299.  
  300.     if (i == j || i < 0)
  301.     return (i);
  302. #ifdef HAVEDUP2
  303.     if (j >= 0) {
  304.     (void) dup2(i, j);
  305.     if (j != i)
  306.         (void) close(i);
  307.     return (j);
  308.     }
  309. #endif
  310.     j = dcopy(i, j);
  311.     if (j != i)
  312.     (void) close(i);
  313.     return (j);
  314. }
  315.  
  316. int
  317. dcopy(i, j)
  318.     register int i, j;
  319. {
  320.  
  321.     if (i == j || i < 0 || (j < 0 && i > 2))
  322.     return (i);
  323. #ifdef HAVEDUP2
  324.     if (j >= 0) {
  325.     (void) dup2(i, j);
  326.     return (j);
  327.     }
  328. #endif
  329.     (void) close(j);
  330.     return (renum(i, j));
  331. }
  332.  
  333. static int
  334. renum(i, j)
  335.     register int i, j;
  336. {
  337.     register int k = dup(i);
  338.  
  339.     if (k < 0)
  340.     return (-1);
  341.     if (j == -1 && k > 2)
  342.     return (k);
  343.     if (k != j) {
  344.     j = renum(k, j);
  345.     (void) close(k);
  346.     return (j);
  347.     }
  348.     return (k);
  349. }
  350.  
  351. void
  352. copy(to, from, size)
  353.     register char *to, *from;
  354.     register int size;
  355. {
  356.  
  357.     if (size && from && to)
  358.     do
  359.         *to++ = *from++;
  360.     while (--size != 0);
  361. }
  362.  
  363. /*
  364.  * Left shift a command argument list, discarding
  365.  * the first c arguments.  Used in "shift" commands
  366.  * as well as by commands like "repeat".
  367.  */
  368. void
  369. lshift(v, c)
  370.     register Char **v;
  371.     register int c;
  372. {
  373.     register Char **u;
  374.  
  375.     for (u = v; *u && --c >= 0; u++)
  376.     xfree((ptr_t) *u);
  377.     (void) blkcpy(v, u);
  378. }
  379.  
  380. int
  381. number(cp)
  382.     Char   *cp;
  383. {
  384.     if (!cp)
  385.     return (0);
  386.     if (*cp == '-') {
  387.     cp++;
  388.     if (!Isdigit(*cp))
  389.         return (0);
  390.     cp++;
  391.     }
  392.     while (*cp && Isdigit(*cp))
  393.     cp++;
  394.     return (*cp == 0);
  395. }
  396.  
  397. Char  **
  398. copyblk(v)
  399.     register Char **v;
  400. {
  401.     register Char **nv =
  402.     (Char **) xcalloc((size_t) (blklen(v) + 1), sizeof(Char **));
  403.  
  404.     return (blkcpy(nv, v));
  405. }
  406.  
  407. #ifndef SHORT_STRINGS
  408. char   *
  409. strend(cp)
  410.     register char *cp;
  411. {
  412.     if (!cp)
  413.     return (cp);
  414.     while (*cp)
  415.     cp++;
  416.     return (cp);
  417. }
  418.  
  419. #endif                /* SHORT_STRINGS */
  420.  
  421. Char   *
  422. strip(cp)
  423.     Char   *cp;
  424. {
  425.     register Char *dp = cp;
  426.  
  427.     if (!cp)
  428.     return (cp);
  429.     while (*dp++ &= TRIM)
  430.     continue;
  431.     return (cp);
  432. }
  433.  
  434. void
  435. udvar(name)
  436.     Char   *name;
  437. {
  438.  
  439.     setname(short2str(name));
  440.     stderror(ERR_NAME | ERR_UNDVAR);
  441. }
  442.  
  443. int
  444. prefix(sub, str)
  445.     register Char *sub, *str;
  446. {
  447.  
  448.     for (;;) {
  449.     if (*sub == 0)
  450.         return (1);
  451.     if (*str == 0)
  452.         return (0);
  453.     if (*sub++ != *str++)
  454.         return (0);
  455.     }
  456. }
  457.